﻿Public Class FormEditCheckOut
    Dim DAReservationDetail As New DSReservationTableAdapters.RESERVATION_DETAILTableAdapter
    Dim DAReservation As New DSReservationTableAdapters.RESERVATIONTableAdapter
    Dim DARoom As New DSFloorManagemantTableAdapters.ROOMSTableAdapter
    Dim DAInvoiceReservation As New DSReservationTableAdapters.View_ReservationInvoiceTableAdapter
    Private Sub FormEditCheckOut_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshReservationDetail(LblReservation.Text)
        TxtTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
        TxtSubTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
    End Sub

    Public Sub RefreshReservationDetail(ByVal ReservationID As Integer)
        GridReservationDedail.DataSource = DAReservationDetail.SelectReservationByID(ReservationID)
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Close()
    End Sub
    Private Function TotalService(ByVal ServiceRoom As Integer) As Double
        Dim TblService As DataTable = DAReservationDetail.SelectReservationByID(LblReservation.Text)
        Dim Total As Double = 0
        For Each row In TblService.Rows
            Total = Total + CDbl(row("TOTAL_PRICE"))
        Next
        Return Total + ServiceRoom
    End Function
    Private Sub BtnDeleteService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDeleteService.Click
        If GridReservationDedail.SelectedItems.Count = 0 Then Exit Sub
        If MessageBox.Show("តើអ្នកចង់លប់ សេវាកម្មនេះ?", "Delete service", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            If DAReservationDetail.DeleteService(GridReservationDedail.GetRow.Cells("RESERVATION_DE_ID").Value, GridReservationDedail.GetRow.Cells("SERVICE_ID").Value) = 1 Then
                RefreshReservationDetail(LblReservation.Text)
                TxtTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
                TxtSubTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
            End If
        End If
    End Sub

    Private Sub TxtDiscound_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtDiscound.KeyUp
        If CDbl(EmptyString(TxtDiscound.Text)) > CDbl(TxtTotal.Text) Then
            MessageBox.Show("អ្នកមិនបញ្ចុះតំលៃបានទេ ព្រោះ discount ធំជាង", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            TxtDiscound.Text = 0
            TxtSubTotal.Text = CDbl(TxtTotal.Text)
        Else
            TxtSubTotal.Text = CDbl(TxtTotal.Text) - CDbl(EmptyString(TxtDiscound.Text))
        End If
    End Sub

    Private Sub TxtDiscound_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtDiscound.KeyPress
        SetDisableKeyString(e)
    End Sub

    Private Sub txtPriceRoom_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPriceRoom.KeyUp
        'If CDbl(txtPriceRoom.Text) <= 0 Then
        '    MessageBox.Show("អ្នកមិនអាច បញ្ចូលតំលៃតូចជាង ឬស្មើ 0បានទេ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        '    Exit Sub
        'End If
        TxtTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
        TxtSubTotal.Text = TotalService(EmptyString(txtPriceRoom.Text))
    End Sub

    Private Sub txtPriceRoom_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPriceRoom.KeyPress
        SetDisableKeyString(e)
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If ValidateTextField(txtPriceRoom, "", ErrCheckOut) = False Then Exit Sub
        If CDbl(txtPriceRoom.Text) <= 0 Then
            MessageBox.Show("អ្នកមិនអាច បញ្ចូលតំលៃតូចជាង ឬស្មើ 0បានទេ!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        If MessageBox.Show("Do you want save?", "Save", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            'If DAReservation.CheckOutReservation(TimeOut.Value.Date, TimeOut.Value, txtPriceRoom.Text, 2, 1, EmptyString(TxtDiscound.Text), EmptyString(TxtSubTotal.Text), EmptyString(TxtTotal.Text), USER_NAME, LblReservation.Text) = 1 Then
            '    'Update Room Status
            '    DARoom.UpdateRoomStatus(0, lblRoomID.Text)

            '    'Add More Room Service to Reservation Detail
            '    DAReservationDetail.DeleteService(GridReservationDedail.GetRow.Cells("RESERVATION_DE_ID").Value, 77)
            '    DAReservationDetail.InsertNewReservation(LblReservation.Text, 77, "តំលៃស្នាក់នៅ", 1, EmptyString(txtPriceRoom.Text), EmptyString(txtPriceRoom.Text))
            '    'Try
            '    '    Dim Invoice As New Invice
            '    '    Dim tblReservation As DataTable = DAInvoiceReservation.GetInvoiceByReservationID(LblReservation.Text)
            '    '    Invoice.SetDataSource(tblReservation)
            '    '    printDocument.PrinterSettings.PrinterName = PrinterReceipt.Trim
            '    '    Invoice.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName
            '    '    Invoice.PrintToPrinter(1, False, 1, 1)
            '    '    Invoice.Dispose()
            '    '    Invoice.Close()

            '    'Catch ex As Exception

            '    'End Try

            '    Me.DialogResult = Windows.Forms.DialogResult.OK
            'Else
            '    MessageBox.Show("Error save Service Check out", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'End If

        End If
    End Sub
End Class